﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrainTechLLC.WebCamLib3;

namespace WpfImageReceiver
{
    /// <summary>
    /// Interaction logic for CameraHandlerView.xaml
    /// </summary>
    public partial class CameraHandlerView : UserControl
    {
        public static readonly DependencyProperty CameraHandlerViewModelProperty =
           DependencyProperty.Register("CameraHandlerViewModel", typeof(CameraHandlerViewModel), typeof(CameraHandlerView), new PropertyMetadata(null, OnCameraHandlerViewModelPropertyChanged));

        public CameraHandlerViewModel CameraHandlerViewModel
        {
            get { return (CameraHandlerViewModel)GetValue(CameraHandlerViewModelProperty); }
            set { SetValue(CameraHandlerViewModelProperty, value); }
        }

        private static void OnCameraHandlerViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CameraHandlerView source = d as CameraHandlerView;
            CameraHandlerViewModel vNew = e.NewValue as CameraHandlerViewModel;
            CameraHandlerViewModel vOld = e.OldValue as CameraHandlerViewModel;
            source.UpdateCameraHandlerViewModel(vNew, vOld);
        }

        private void UpdateCameraHandlerViewModel(CameraHandlerViewModel viewModel, CameraHandlerViewModel viewModelOld)
        {
            if (viewModel == viewModelOld)
                return;
            
            CameraHandlerViewModel = viewModel;

            // Let the UI bind to the view-model.
            this.DataContext = CameraHandlerViewModel;
        }

        public CameraHandlerView()
        {
            InitializeComponent();
        }
    }
}
